<template>
  <a-modal
    :visible="visible"
    :title="$t('key.reviewPlanDetail')"
    :okText="isEdit ? $t('button.submitReviewResults') : $t('button.print')"
    :cancelText="$t('button.close')"
    :confirmLoading="confirmLoading"
    :width="1200"
    :bodyStyle="{
      'max-height': '76vh',
      overflow: 'auto'
    }"
    style="top: 20px;"
    @cancel="handleCancel"
    @ok="submitCheck"
    >
      <a-spin :spinning="spinning">
        <detail-list :col="2">
          <detail-list-item :term="$t('key.name')">{{ planInfo.pyjh.xm }}&emsp;</detail-list-item>
          <detail-list-item :term="$t('key.xh')">{{ planInfo.pyjh.xh }}&emsp;</detail-list-item>
          <detail-list-item :term="$t('key.pydw')">{{ planInfo.pyjh.dwmc }}&emsp;</detail-list-item>
          <detail-list-item :term="$t('key.zy')">{{ planInfo.pyjh.zymc }}&emsp;</detail-list-item>
          <detail-list-item :term="$t('key.nj')">{{ planInfo.pyjh.sznj }}&emsp;</detail-list-item>
          <detail-list-item :term="$t('key.pyfa')">{{ planInfo.pyjh.pyfamc }}&emsp;</detail-list-item>
        </detail-list>
        <a-table
          rowKey="id"
          :columns="columns"
          :dataSource="planInfo.pyjhkcList"
          :pagination="false"
          :scroll="{x: 1000}"
        >
          <span slot="serial" slot-scope="text, record, index">
            {{ index + 1 }}
          </span>
          <template slot="shzt" slot-scope="shzt, record">
            <template v-if="isEdit">
              <span v-if="record.sfmbkc == 1">
                {{ record.kcshzt == 2 ? record.kcshztmc : "已审核" }}
              </span>
              <a-radio-group v-else v-model="record.kcshzt">
              <!-- <a-radio-group v-else @change="e => handleChange(e, record)" :defaultValue="record.kcshzt == '01' || '02' ? '02' : '03'"> -->
                <!-- <a-radio :value="'01'">{{ '未审核' }}</a-radio> -->
                <a-radio value="02">{{ $t('key.approved') }}</a-radio>
                <a-radio value="03">{{ $t('key.fail') }}</a-radio>
              </a-radio-group>
            </template>
            <span v-else>{{ record.kcshztmc }}</span>
          </template>
          
          <template slot="shyj" slot-scope="shyj, record">
            <a-input v-if="isEdit" v-model="record.shyj"/>
            <span v-else>{{ record.shyj }}</span>
          </template>
          
        </a-table>

        <credit-table :pyjh="planInfo.pyjh"/>
      </a-spin>
  </a-modal>
</template>

<script>
import DetailList from '~/tool/DetailList'
import creditTable from '../../components/creditTable'
const DetailListItem = DetailList.Item

export default {
  props: ['visible'],
  data () {
    return {
      planInfo: {
        pyjh: {},
        pyjhkcList: []
      },
      isEdit: true,
      columns: [
        {
          title: this.$t('key.no'),
          width: '80px',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: this.$t('key.kcdm'),
          width: '100px',
          dataIndex: 'kcdm'
        },
        {
          title: this.$t('key.kcmc'),
          width: '110px',
          dataIndex: 'kcmc'
        },
        {
          title: this.$t('key.xs'),
          width: '63px',
          dataIndex: 'xs'
        },
        {
          title: this.$t('key.xf'),
          width: '63px',
          dataIndex: 'xf'
        },
        {
          title: this.$t('key.xkyq'),
          width: '92px',
          dataIndex: 'bxhxxmc'
        },
        {
          title: this.$t('key.kclx'),
          width: '92px',
          dataIndex: 'kclxmc'
        },
        {
          title: this.$t('key.sfxwk'),
          width: '98px',
          dataIndex: 'sfxwkmc'
        },
        {
          title: this.$t('key.shzt'),
          dataIndex: 'shzt',
          width: '160px',
          scopedSlots: { customRender: 'shzt' }
        },
        {
          title: this.$t('key.shyj'),
          dataIndex: 'shyj',
          width: '141px',
          scopedSlots: { customRender: 'shyj' }
        }
      ],
      confirmLoading: false,
      spinning: true
    }
  },
  components: {
    DetailList,
    DetailListItem,
    creditTable
  },
  methods: {
    getInfo (id, isEdit) {
      this.isEdit = isEdit
      this.$api.cultivate.pyjhDetail(id).then(res => {
        this.spinning = false
        this.planInfo = res.data
        if(isEdit) {
          this.planInfo.pyjhkcList.forEach(kc => {
            if (kc.sfmbkc != '1') {
              delete kc.kcshzt
            }
          })
        }
      })
    },
    handleChange (e, kc) {
      // console.log(e.target.value)
      kc.kcshzt = e.target.value
    },
    submitCheck () {
      if (this.isEdit) {
        // // this.planInfo.pyjhkcList.map(i => i.kcshzt = i.kcshzt == '03' ? '03' : '02')
        this.planInfo.pyjhkcList.map(i => {
          if (i.sfmbkc == '1' ) {
            i.kcshzt = '02'
          }
        })
        console.log(this.planInfo.pyjhkcList)
        let a = this.planInfo.pyjhkcList.map(i => {
          return i.sfmbkc == 1 ? true : i.kcshzt == '02' || i.kcshzt == '03'
        })
        let index = a.findIndex(i => i == false)
        console.log(index)
        if (index == -1) {
          this.confirmLoading = true
          let params = {
            pyjhId: this.planInfo.pyjh.id,
            pyjhkcshList: this.planInfo.pyjhkcList.map(kc => {
              return {
                kcshzt: kc.kcshzt,
                pyjhkcId: kc.id,
                shyj: kc.shyj ? kc.shyj : ''
              }
            })
          }
          this.$api.cultivate.pyjhCheck(params).then(res => {
            this.$message.success(res.msg)
            this.$emit('cancel')
            this.$emit('refresh')
          }).finally(() => {
            this.confirmLoading = false
          })
        } else {
          this.$message.warn(this.$t('desc.hintCheckStatus'))
        }
      } else {
        // this.$message.warning("该功能暂未实现")
        this.handlePrint()
      }
    },
    handleCancel() {
      this.$emit('cancel')
      this.spinning = true
    },
    resetSelect() {
      this.$nextTick(() => {
        this.$refs.table.selectedRows = []
        this.$refs.table.selectedRowKeys = []
      })
    },
    handlePrint() {
      // if (!this.selectedRows.length) return this.$message.info('请选择要打印的计划！');
      // let ids = this.selectedRows.map(o => o.id).toString()
      let ids = this.planInfo.pyjh.id
      this.$downloadPdf('/teacherClient/pyjh/pyjhPrint', { ids }, '培养计划.pdf')
      this.resetSelect()

    }
  }
}
</script>

<style scoped lang="less">
.kc-table{
  margin: 10px auto;
  td{
    padding: 5px;
  }
}

.spin-content{
  background-color: #e6f7ff;
}

// .ant-radio-group {
//   display: flex;
// }
</style>

<style lang="less">
.ant-table-body {
  width: 101% !important;
}
</style>
